package edu.ccut.saturn.studio.server.dialog;

import java.io.File;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.server.model.SaturnServer;

public class NewSaturnServerDialog extends TitleAreaDialog {

	private Text portTxt;
	private static final String DEFAULT_SELECTED_PATH = Messages
			.getString("NewSaturnServerDialog.0"); //$NON-NLS-1$

	private Text textServerPath;
	private Text textSaturnPath;
	private Combo comboServerType;
	private Text textName;
	private String serverName;

	Map<String, SaturnServer> saturnServers = null;

	public NewSaturnServerDialog(Shell shell,
			Map<String, SaturnServer> saturnServers) {
		super(shell);

		this.saturnServers = saturnServers;
	}

	public String getSaturnServerName() {
		return this.serverName;
	}

	protected Point getInitialSize() {
		return new Point(689, 316);
	}

	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		final GridData gd_container = new GridData(SWT.FILL, SWT.FILL, false,
				false);
		gd_container.heightHint = 166;
		container.setLayoutData(gd_container);

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages
				.getString("NewSaturnServerDialog.SaturnServiceName")); //$NON-NLS-1$

		textName = new Text(container, SWT.BORDER);
		textName.setTextLimit(50);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		textName.setLayoutData(gd_textName);
		new Label(container, SWT.NONE);

		final Label label_1 = new Label(container, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 10;
		label_1.setLayoutData(gd_label_1);
		label_1.setText(Messages.getString("NewSaturnServerDialog.ServerType")); //$NON-NLS-1$

		comboServerType = new Combo(container, SWT.READ_ONLY);
		final GridData gd_comboServerType = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_comboServerType.verticalIndent = 10;
		comboServerType.setLayoutData(gd_comboServerType);
		new Label(container, SWT.NONE);
		comboServerType.setItems(SaturnServer.SERVER_TYPES);

		comboServerType.select(0);

		final Label webLabel = new Label(container, SWT.NONE);
		final GridData gd_webLabel = new GridData();
		gd_webLabel.verticalIndent = 10;
		webLabel.setLayoutData(gd_webLabel);
		webLabel.setText(Messages
				.getString("NewSaturnServerDialog.WebContainer")); //$NON-NLS-1$

		textServerPath = new Text(container, SWT.BORDER);
		textServerPath.setTextLimit(100);
		textServerPath.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textServerPath = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_textServerPath.verticalIndent = 10;
		textServerPath.setLayoutData(gd_textServerPath);

		final Button button_1 = new Button(container, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(Display
						.getDefault().getShells()[0]);
				dialog.setText(Messages
						.getString("NewSaturnServerDialog.Catalog")); //$NON-NLS-1$
				dialog.setMessage(Messages
						.getString("NewSaturnServerDialog.PleaseSelectCatalog")); //$NON-NLS-1$

				String dir = textServerPath.getText();

				if (Messages
						.getString("NewSaturnServerDialog.1").equals(dir.trim())) { //$NON-NLS-1$
					dir = DEFAULT_SELECTED_PATH;
				}
				dialog.setFilterPath(dir); //$NON-NLS-1$

				dir = dialog.open();
				if (dir != null) {
					dir = dir.replace(File.separator, Messages
							.getString("NewSaturnServerDialog.2")); //$NON-NLS-1$
					if (dir.length() > 100) {
						MessageDialog
								.openInformation(
										null,
										Messages
												.getString("NewSaturnServerDialog.remend1"), //$NON-NLS-1$
										Messages
												.getString("NewSaturnServerDialog.WebLength")); //$NON-NLS-1$
						return;
					}
					textServerPath.setText(dir);
					textSaturnPath.setText(SaturnServer
							.getDefaultSaturnServerPath(dir, comboServerType
									.getText())); //$NON-NLS-1$
				}
			}
		});
		final GridData gd_button_1 = new GridData();
		gd_button_1.verticalIndent = 10;
		button_1.setLayoutData(gd_button_1);
		button_1.setText(Messages.getString("NewSaturnServerDialog.Browse")); //$NON-NLS-1$

		final Label label_3 = new Label(container, SWT.NONE);
		final GridData gd_label_3 = new GridData();
		gd_label_3.verticalIndent = 10;
		label_3.setLayoutData(gd_label_3);
		label_3.setText(Messages
				.getString("NewSaturnServerDialog.TiatnservicePosition")); //$NON-NLS-1$

		textSaturnPath = new Text(container, SWT.BORDER);
		textSaturnPath.setTextLimit(100);
		textSaturnPath.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_textSaturnPath = new GridData(SWT.FILL, SWT.CENTER,
				true, false);
		gd_textSaturnPath.verticalIndent = 10;
		textSaturnPath.setLayoutData(gd_textSaturnPath);

		final Button button = new Button(container, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(Display
						.getDefault().getShells()[0]);
				dialog.setText(Messages
						.getString("NewSaturnServerDialog.Catalog")); //$NON-NLS-1$
				dialog.setMessage(Messages
						.getString("NewSaturnServerDialog.PlaeseSelectCatalog")); //$NON-NLS-1$
				String dir = textSaturnPath.getText();

				if (Messages
						.getString("NewSaturnServerDialog.3").equals(dir.trim())) { //$NON-NLS-1$
					dir = DEFAULT_SELECTED_PATH;
				}
				dialog.setFilterPath(dir);

				dir = dialog.open();
				if (dir != null) {
					if (dir.length() > 100) {
						MessageDialog
								.openInformation(
										null,
										Messages
												.getString("NewSaturnServerDialog.remend2"), //$NON-NLS-1$
										Messages
												.getString("NewSaturnServerDialog.SaturnServerLength")); //$NON-NLS-1$
						return;
					}
					dir = dir.replace(File.separator, Messages
							.getString("NewSaturnServerDialog.4")); //$NON-NLS-1$
					textSaturnPath.setText(dir);
				}
			}
		});

		final GridData gd_button = new GridData();
		gd_button.verticalIndent = 10;
		button.setLayoutData(gd_button);
		button.setText(Messages.getString("NewSaturnServerDialog.Browse")); //$NON-NLS-1$

		final Label label_2 = new Label(container, SWT.NONE);
		final GridData gd_label_2 = new GridData();
		gd_label_2.verticalIndent = 10;
		label_2.setLayoutData(gd_label_2);
		label_2.setText(Messages.getString("NewSaturnServerDialog.serverPart")); //$NON-NLS-1$

		portTxt = new Text(container, SWT.BORDER);
		portTxt.setTextLimit(50);
		portTxt.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate();
			}
		});
		final GridData gd_portTxt = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		portTxt.setLayoutData(gd_portTxt);
		new Label(container, SWT.NONE);

		setTitle(Messages.getString("NewSaturnServerDialog.serverTitle")); //$NON-NLS-1$
		setMessage(Messages.getString("NewSaturnServerDialog.WebSaturnServer") + Messages.getString("NewSaturnServerDialog.5") + Messages.getString("NewSaturnServerDialog.pathLength")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		return area;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			String name = textName.getText().trim();
			if (validate()) {
				SaturnServer server = new SaturnServer();
				server.setName(name);
				server.setClassType(comboServerType.getText().trim());
				server.setServerPath(textServerPath.getText().trim());
				server.setDepolyPath(textSaturnPath.getText().trim());
				server.setPort(portTxt.getText().trim());
				saturnServers.put(name, server);
				this.serverName = name;
				super.buttonPressed(buttonId);
			}
		}
		if (buttonId == IDialogConstants.CANCEL_ID) {
			super.cancelPressed();
		}
	}

	private boolean validate() {
		String serverName = textName.getText().trim();
		String port = portTxt.getText().trim();

		if (saturnServers.containsKey(serverName)) {
			setErrorMessage(Messages
					.getString("NewSaturnServerDialog.NameIsExist")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		if (SaturnValidation.isEmpty(serverName)) {
			setErrorMessage(Messages
					.getString("NewSaturnServerDialog.NameIsEmpty")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		if (SaturnValidation.isBadWord(serverName)) {
			setErrorMessage(Messages
					.getString("NewSaturnServerDialog.NameIsBadword")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		if (!SaturnValidation.isInt(port)) {
			setErrorMessage(Messages
					.getString("NewSaturnServerDialog.PortIsInt")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return false;
		}

		setErrorMessage(null);
		this.getButton(IDialogConstants.OK_ID).setEnabled(true);
		return true;

	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("ManageConnectionDialog.confirm"), false); //$NON-NLS-1$
		this.getButton(IDialogConstants.OK_ID).setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("ManageConnectionDialog.cancel"), false); //$NON-NLS-1$
	}
}
